<?php

namespace App\Service\Pay\SysType;

use App\Enums\OpenShop\InvestmentPromotion\ErrCode;
use App\Enums\OpenShop\InvestmentPromotion\MainProcessEnum;
use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Enums\OpenShop\MissionTemplateEnum;
use App\Events\OpenShop\OpenShopProcessEvent;
use App\Events\OpenShop\SignSiteMissionEvent;
use App\Exceptions\Custom\CustomHttpResponseException;
use App\Library\FeiShu\SendFs;
use App\Models\Appraise\AppraiseModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSiteMissionModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSpaceDesignCostMappingModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSpaceDesignModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSpaceDesignSoftOutfitMappingModel;
use App\Service\OpenShop\Feishu\ShopTagsService;
use App\Service\OpenShop\Import\HistoryOrder\HistoryOrderParam;
use App\Service\WorkOrder\Appraise\NotifyService;
use Kuafu\GeneralFs\BuildFs;

class PayDecorateService extends BasePaySysTypeService
{
    public function valid(): void
    {
        // 订单已作废
        if ($this->signOrderModel->status == SignOrderEnum::ORDER_STATUS_NO) {
            throw new CustomHttpResponseException(
                ErrCode::errCodeTrans(ErrCode::CODE_SIGN_ORDER_STATUS_DEL)
            );
        }

        // 订单状态不对
        if ($this->orderModel->orderStatus != OpenShopSpaceDesignSoftOutfitMappingModel::ORDER_STATUS_0) {
            throw new CustomHttpResponseException(
                ErrCode::errCodeTrans(ErrCode::CODE_ERR_DESIGN_ORDER_STATUS)
            );
        }

        // 支付状态不对
        if (!in_array($this->orderModel->payStatus, [
            OpenShopSpaceDesignSoftOutfitMappingModel::ORDER_PAY_STATUS_0,
            OpenShopSpaceDesignSoftOutfitMappingModel::ORDER_PAY_STATUS_1
        ])) {
            throw new CustomHttpResponseException(
                ErrCode::errCodeTrans(ErrCode::CODE_ERR_DESIGN_PAY_STATUS)
            );
        }
    }

    public function priceLists(): array
    {
        return collect([
            [
                'name'  => '软装报价',
                'price' => $this->orderModel->fee,
            ],
        ])->filter(function ($item) {
            if ($item['price'] == 0) {
                return false;
            }

            return true;
        })->values()->toArray();
    }

    public function paySuccessCheck(): bool
    {
        // 主订单已支付完成、直接返回成功 - 幂等
        if ($this->orderModel->payStatus >= OpenShopSpaceDesignSoftOutfitMappingModel::ORDER_PAY_STATUS_2) {
            return true;
        }

        return false;
    }

    public function paySuccessSaveOrder(int $amount): void
    {
        $this->orderModel->onePaySuccess($amount);
    }

    // 软装费支付成功要干的事
    // 2023-05-01之前的逻辑，后订单迁至kfos中
    public function paySuccessAfter(): void
    {
        // 已废弃！！！

        if ($this->orderModel->orderStatus != OpenShopSpaceDesignSoftOutfitMappingModel::ORDER_STATUS_1) {
            return;
        }

        // 更新软装标签
        (new ShopTagsService($this->signOrderModel->orderNo))->updateSoftPayTag();

        if (HistoryOrderParam::$forceCurrentUser) {
            $uid = HistoryOrderParam::$forceCurrentUser->id;
        } else {
            $uid = $this->signOrderModel->merchantUserId;
        }

        // 单笔部分支付
        if ($this->orderModel->payStatus == OpenShopSpaceDesignSoftOutfitMappingModel::ORDER_PAY_STATUS_1) {

            // 流程日志
            event(new OpenShopProcessEvent($uid, $this->signOrderModel->mainProcessId,
                MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN, MainProcessEnum::SPACE_DESIGN_STEP_TYPE_SPACE_FINISH));
            return;
        }

        $main = OpenShopMainProcessModel::query()
                                        ->where('orderNo', $this->signOrderModel->orderNo)
                                        ->first();

        $nowStepStatus = $main->nowStepStatus;

        if ($nowStepStatus != MissionTemplateEnum::STEP_STATUS_17) {

            // 支线的流程 - 支付成功，关闭任务即可
            OpenShopSiteMissionModel::finishBranchMission($this->signOrderModel->orderNo,
                MissionTemplateEnum::STEP_STATUS_17);
        } else {

            // 更新space_design表step
            OpenShopSpaceDesignModel::updateStep($this->signOrderModel->orderNo,
                MainProcessEnum::SPACE_DESIGN_STEP_TYPE_SPACE_FINISH);
            // 正常流程 - 支付成功，进行下一个任务
            event(new SignSiteMissionEvent($this->signOrderModel->orderNo, MissionTemplateEnum::STEP_STATUS_17,
                $this->signOrderModel->merchantUserId));

            $missionList = OpenShopMainProcessModel::where('orderNo', $this->signOrderModel->orderNo)
                                                   ->first()->stepStatusLists;
            $missionList = json_decode($missionList, true);

            $step = MainProcessEnum::MAIN_PROCESS_ENGINEER_CONSTRUCT;
            // 更新主订单流程
            OpenShopMainProcessModel::query()->where('orderNo', $this->signOrderModel->orderNo)->update([
                'step' => $step
            ]);

            //筹备阶段-在「软装模块支付」完成之后，进行推送 评价消息
            NotifyService::notify($this->signOrderModel->orderNo, AppraiseModel::TYPE_OPENSHOP_PREPARATION, 0, $this->signOrderModel->progressRel->shopId);
        }
        // 流程日志
        event(new OpenShopProcessEvent($uid, $this->signOrderModel->mainProcessId,
            MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN, MainProcessEnum::SPACE_DESIGN_STEP_TYPE_SPACE_FINISH));
    }
}
